package com.zhihuichongcheng.application.ui.cricle.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.cricle.CricleCategoryBean;
import com.zhihuichongcheng.application.bean.cricle.CricleImageInfo;
import com.zhihuichongcheng.application.bean.cricle.HealthBean;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.BaseApplication;
import com.zhihuichongcheng.application.ui.cricle.activity.CricleDetailActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/28.
 */

public class MyCricleAdapter extends RecyclerView.Adapter<MyCricleAdapter.ViewHold> {

    private Context mContext;
    private List<HealthBean> mListData = new ArrayList<>();
    private UserBean mUser;
    public MyCricleAdapter(Context context)
    {
        this.mContext = context;
        BaseApplication app = (BaseApplication)mContext.getApplicationContext();
        mUser = app.getUser();
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_cricle_healthy,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final HealthBean bean = mListData.get(position);
        holder.mTvContent.setText(bean.getContent());
        holder.mTvTitle.setText(bean.getTitle());
        List<CricleImageInfo> list = bean.getImgList();
        if(list!=null&&list.size()>0)
        {
            Glide.with(mContext).load(list.get(0).getJinbaoHomeImgUrl()).into(holder.mImCricleImg);
            holder.mImCricleImg.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.mImCricleImg.setVisibility(View.GONE);
        }

        Date date = DateUtil.parseStringToDate(bean.getCreateTime(),"yyyy年MM月dd日 HH:mm:ss");

        holder.mTvTime.setText(DateUtil.format(date));

        UserBean user = bean.getUser();
        if(user!=null)
        {
            holder.mTvName.setText(user.getUserName());
            Glide.with(mContext).load(user.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);
        }
        CricleCategoryBean categoryBean = bean.getJinbaoHomeCategory();
        if(categoryBean!=null)
        {
            holder.mTvType.setText(categoryBean.getCategoryName());
        }
        holder.mTvDel.setVisibility(View.VISIBLE);
        holder.mTvCommentNum.setText("评论："+bean.getCommentCount());
        holder.mTvPariseNum.setText("赞："+bean.getLikeCount());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,CricleDetailActivity.class);
                intent.putExtra("homeId",bean.getJinbaoHomeId());
                mContext.startActivity(intent);
            }
        });

        holder.mTvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpManager manager = HttpManager.getInstance(mContext);
                HashMap<String,String> params = new HashMap<String, String>();
                params.put("token",mUser.getToken());
                params.put("jinbaoHomeId",bean.getJinbaoHomeId());
                manager.post(NetworkAPI.DelCricle, new HttpCallback() {
                    @Override
                    public void onSuccess(int code, String value, String msg) {
                        if(code ==1)
                        {
                            mListData.remove(position);
                            notifyDataSetChanged();
                        }
                        else
                        {
                            ToastOpt.createToast(mContext,"删除失败");
                        }
                    }

                    @Override
                    public void onFailure(String value) {
                        ToastOpt.createToast(mContext,"删除失败");
                    }
                },params);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<HealthBean> list) {
        this.mListData.addAll(list);
    }

    public void clear() {
        mListData.clear();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private ImageView mImAvatar,mImCricleImg;
        private View view;
        private TextView mTvDel;
        private TextView mTvName,mTvTime,mTvTitle,mTvContent,mTvType,mTvCommentNum,mTvPariseNum;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImAvatar = (ImageView)itemView.findViewById(R.id.im_avatar);
            mTvName = (TextView)itemView.findViewById(R.id.tv_name);
            mTvTime = (TextView)itemView.findViewById(R.id.tv_time);
            mTvTitle = (TextView)itemView.findViewById(R.id.tv_title);
            mTvContent = (TextView)itemView.findViewById(R.id.tv_content);
            mTvType = (TextView)itemView.findViewById(R.id.tv_type);
            mTvCommentNum = (TextView)itemView.findViewById(R.id.tv_comment_num);
            mTvPariseNum = (TextView)itemView.findViewById(R.id.tv_parise_num);
//            mImCricleImg = (ImageView)itemView.findViewById(R.id.im_cricleImg);
            mTvDel = (TextView)itemView.findViewById(R.id.tv_del);
        }
    }
}
